#include "lsDispose.h"


void lsDispose(int sockfd,char * str){
    
    str[strlen(str)-1] = '\0';

    char command[] = "ls";
    
    
    Train train;
    train.length = strlen(command);            //向后台发送puts信号
    memcpy(train.data,command,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    train.length = strlen(str);         //发送服务器该传输的位置(父文件路径)
    memcpy(train.data,str,train.length);
    send(sockfd,&train,sizeof(train.length)+train.length,MSG_NOSIGNAL);
    
    int epollfd = epoll_create(1);
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = sockfd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,sockfd,&event) ;
    struct epoll_event readyset[5];
    epoll_wait(epollfd,readyset,5,-1);
    

    char has;
    recv(readyset[0].data.fd,&has,1,MSG_WAITALL);
    if(has == '0'){
        printf("null dir\n");
        return;
    }

    char filename[1024] ;
    while(1){
        bzero(filename,sizeof(filename));
        recv(readyset[0].data.fd,&train.length,sizeof(train.length),MSG_WAITALL);
        if(train.length == 0){
            break;
        }
        recv(readyset[0].data.fd,train.data,train.length,MSG_WAITALL);
        memcpy(filename,train.data,train.length);
        char type;
        recv(readyset[0].data.fd,&type,1,MSG_WAITALL);
        if(type == 'f'){
            printf("%s   ",filename);
        }else{
            printf(BULE "%s   " RESET,filename);
        }
    }
    printf("\n");
    return ;

}

